package Graph;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class TestPlanarity {

	public static void main(String[] args){
		for (int i = 1; i< 100;i++){
		File f = new File("C:\\Documents and Settings\\T\\Desktop\\input\\input\\n7");
//		File f = new File("/Users/txb7419/TestGraph");
//		File f = new File(args[0]);
		Graph g = null;
		Graph c = null;
		System.out.println("\n\n\n*****************************\n" + f.getAbsolutePath());
		g = new Graph(f.getAbsolutePath());
		c = g.FindCycle();
//		System.out.println(g.testPath());
		
//		Graph.print(c);
		
		boolean test = false;
		if (test){
//		System.out.println(g);
//		System.out.println("Cycle: " + c);
////		List<Vertex> path = g.findPath(g.getVertex("4"), g.getVertex("6"));
////		System.out.println(path);
////		path = g.findLongPath(g.getVertex("4"), g.getVertex("6"));
////		System.out.println(path);
		List<Graph> cc = g.findPieces(c);
//		System.out.println("Connected Components (Pieces): " + cc);
//		System.out.println("Piece: " + cc.get(0));
		List<Vertex> attachPts = c.getAttachmentPoints(cc.get(0));
//		System.out.println("Attach Pts: " + attachPts);
		Random r = new Random();
		int a_index = r.nextInt(attachPts.size());
		Vertex a = attachPts.get(a_index);
		Vertex b = attachPts.get((a_index + 1) % attachPts.size());
//		System.out.println("a: " + a + ", b: " + b);
		List<Vertex> piecePath = cc.get(0).findPath(a, b);
		List<Vertex> cyclePath = c.findPath(a, b);
//		
//		System.out.println("CyclePath: " + cyclePath);
//		System.out.println("PiecePath: " + piecePath);
//		
		Graph cPrime = c.replacePath(cyclePath, piecePath);
//		
//		System.out.println("cPrime: " + cPrime);
//		attachPts = c.getAttachmentPoints(cc.get(2));
//		System.out.println("Attach Pts: " + attachPts);
//		List<Vertex> c_path = c.findPath(attachPts.get(0), attachPts.get(1));
//		List<Vertex> p_path = cc.get(0).findPath(attachPts.get(0), attachPts.get(1));
//		Graph newCycle = c.replacePath(c_path, p_path);
//		System.out.println("New Cycle: " + newCycle);
//		System.out.println("Is c a path? " + c.testPath());
//		System.out.println("Is p a path? " + cc.get(0).testPath());
		Graph combine = c.combine(cc.get(0));
//		System.out.println("Combined: " + combine);
//		Graph ccycle = new Graph();
//		
////		Graph ccycle = combine.FindCycle();
//		System.out.println("CCYCLE: " + ccycle);
//
		List<Graph> pieces = combine.findPieces(cPrime);

//		ccycle = combine.FindCycle();
//		System.out.println("CCYCLE: " + ccycle);
//		pieces = combine.findPieces(ccycle);
//		System.out.println("Pieces: " + pieces);
		System.out.println("*********************************");
		List<Graph> morePieces = new LinkedList<Graph>();
		for (Graph piece: pieces){
			System.out.println("\tPiece: " + piece);
			combine = cPrime.combine(piece);
			attachPts = cPrime.getAttachmentPoints(piece);
			a_index = r.nextInt(attachPts.size());
			a = attachPts.get(a_index);
			b = attachPts.get((a_index + 1) % attachPts.size());
			System.out.println("\ta: " + a + ", b: " + b);
			piecePath = piece.findPath(a, b);
			cyclePath = cPrime.findPath(a, b);
			System.out.println("\tCyclePath: " + cyclePath);
			System.out.println("\tPiecePath: " + piecePath);
			Graph cDPrime = cPrime.replacePath(cyclePath, piecePath);
			System.out.println("\tcPrime: " + cPrime);
			System.out.println("\tcDPrime: " + cDPrime);
			List<Graph> somePieces = combine.findPieces(cDPrime);
			System.out.println("\tSome Pieces: " + somePieces);
			System.out.println("*************************************************");
		}
		System.out.println("More Pieces: " + morePieces);
//		Graph combine2 = ccycle.combine(pieces.get(0));
//		Graph dcycle = combine2.FindCycle();
//		List<Graph> pieces2 = combine.findPieces(combine2.FindCycle());
//		

		
	
//		
//		System.out.println("DCYCLE: " + dcycle);
//		System.out.println("Pieces2: " + pieces2);
//		
//		attachPts = dcycle.getAttachmentPoints(pieces2.get(0));
//		System.out.println("Attach Pts: " + attachPts);
//		
		}
		
//		System.out.println(pieces.get(0));
//		System.out.println("Is p a path? " + cc.get(1).testPath());
//		System.out.println("Is p a path? " + cc.get(2).testPath());
//		System.out.println(Graph.TestPlanarity(g, c));
//		Graph ig = g.createInterlacementGraph(c, cc);
//		System.out.println(ig.getVertList());
//		System.out.println(ig.getEdgeList());
//		try{
		System.out.println("IS G PLANAR? " + (Graph.TestPlanarity(g, c, g.findPieces(c))? "YES" : "NO"));
//		}catch(RuntimeException e){
//			e.printStackTrace();
//		}	
		}
	}
}
